#include<iostream>
using namespace std;
#include<vector>
class Solution {
public:
    vector<int> asteroidCollision(vector<int>& asteroids) {
        vector<int> ans;
        for (auto& e : asteroids) {
            if (ans.empty() || e > 0)
                ans.push_back(e);
            else {
                int flag = 0;
                while (!ans.empty() && ans.back() > 0) {
                    int now = ans.back() + e;
                    if (now > 0) {
                        flag = 1;
                        break;
                    }
                    else if (now == 0) {
                        flag = 1;
                        ans.pop_back();
                        break;
                    }
                    else
                        ans.pop_back();
                }
                if (!flag)
                    ans.push_back(e);
            }
        }
        return ans;
    }
};